﻿using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;

namespace Nirvana
{
    [CustomEditor(typeof(ToggleActivator))]
    public class ToggleActivatorEditor : Editor
    {

        private SerializedProperty actives;
        private SerializedProperty deactives;
        private SerializedProperty activesOnly;
        private SerializedProperty deactivesOnly;
        private SerializedProperty transitionMode;
        private SerializedProperty transitionTime;
        private ReorderableList activeslist;
        private ReorderableList deactiveslist;
        private ReorderableList activesOnlylist;
        private ReorderableList deactivesOnlylist;
        public override void OnInspectorGUI()
        {
            base.serializedObject.Update();
            this.activeslist.DoLayoutList();
            this.deactiveslist.DoLayoutList();
            this.activesOnlylist.DoLayoutList();
            this.deactivesOnlylist.DoLayoutList();
            EditorGUILayout.PropertyField(this.transitionMode, new GUILayoutOption[0]);
            if (this.transitionMode.enumValueIndex != 0)
            {
                EditorGUILayout.PropertyField(this.transitionTime, new GUILayoutOption[0]);
            }
            base.serializedObject.ApplyModifiedProperties();
        }

        private void OnEnable()
        {
            if (this.target == null)
            {
                return;
            }
            SerializedObject serializedObject = base.serializedObject;
            this.actives = serializedObject.FindProperty("actives");
            this.deactives = serializedObject.FindProperty("deactives");
            this.activesOnly = serializedObject.FindProperty("activesOnly");
            this.deactivesOnly = serializedObject.FindProperty("deactivesOnly");
            this.transitionMode = serializedObject.FindProperty("transitionMode");
            this.transitionTime = serializedObject.FindProperty("transitionTime");


            InitListUI(ref activeslist, this.actives, "Actives:");
            InitListUI(ref deactiveslist, this.deactives, "Deactive:");
            InitListUI(ref activesOnlylist, this.activesOnly, "Active Only:");
            InitListUI(ref deactivesOnlylist, this.deactivesOnly, "Deactive Only:");

        }

        private void InitListUI(ref ReorderableList list, SerializedProperty prop,string headname)
        {
            list = new ReorderableList(serializedObject, prop);
            list.drawHeaderCallback = (rect) => { GUI.Label(rect, headname); };
            list.elementHeight = 1f * EditorGUIUtility.singleLineHeight;
            list.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) =>
            {
                SerializedProperty element = prop.GetArrayElementAtIndex(index);
                EditorGUI.PropertyField(rect, element);
            };
        }

    }
}

